package nil.ed.lazy.media;

import nil.ed.lazy.media.model.ImageBasicInfo;
import nil.ed.lazy.net.utils.DownloadUtils;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;

/**
 * @author lidelin
 * @since 1.0
 */
public class ImageUtils {

    /**
     * 解析图片链接中图片信息
     * @param imageUrl 图片链接
     * @return 图片基本信息
     * @throws IOException io exp
     */
    public static ImageBasicInfo resolveImage(String imageUrl) throws IOException {
        return resolveImage(DownloadUtils.download(imageUrl));
    }

    /**
     * 解析图片字节流
     * @param image 图片字节流
     * @return 图片基本信息
     * @throws IOException io exp
     */
    public static ImageBasicInfo resolveImage(byte[] image) throws IOException {
        return resolveImage(new ByteArrayInputStream(image));
    }

    /**
     * 解析图片io流
     * @param image 图片io流
     * @return 图片基本信息
     * @throws IOException io exp
     */
    public static ImageBasicInfo resolveImage(InputStream image) throws IOException {
        BufferedImage img = ImageIO.read(image);
        ImageBasicInfo info = new ImageBasicInfo();
        info.setWidth(img.getWidth());
        info.setHeight(img.getHeight());
        return info;
    }

}
